import {url} from '../../util/config'
import $ from 'jquery'
import {message} from "antd";
import login from "../login";

function getNewLogin() {
    flushModuleList();
}

export default function flushModuleList() {
    // 首先react路由 判断 localstorage 有无token,如果没有通过账号密码发送请求拿到token
    let token = localStorage.getItem("token")

    $.ajax({
        async:false,
        headers: {
            'Accept': "application/json",
            'Authorization': token
        },
        url:url.api_url+"Module/GetModuleFlag",
        method:"GET",
        success:function(result){
            if(result.result=="Token過期請重新登錄") {
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                getNewLogin()
            }else {
                console.log(result)
                let arr1 = result.dtFirst.map(item => {
                    return {
                        moduleId: item.MODULE_ID,
                        moduleName: item.MODULE_NAME,
                        moduleParentId: item.MODULE_PARENT_ID,
                        moduleType: item.MODULE_TYPE,
                        moduleUrl: item.MODULE_URL
                    }
                })
                sessionStorage.setItem("getModule",JSON.stringify(arr1))
            }
        },
        error: function (error) {
            message.error("刷新异常")
        }
    });
}
